package c.c.c.a.o;

import android.content.Context;
import android.text.TextUtils;
import c.c.c.a.i.c;
import c.c.c.a.n;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import org.json.JSONException;
import org.json.JSONObject;

/* compiled from: CrashUploader */
public class b {
    public static boolean a = false;

    /* compiled from: CrashUploader */
    public enum a {
        NONE(0),
        GZIP(1),
        DEFLATER(2);

        public a(int i) {
        }
    }

    /* compiled from: CrashUploader */
    public enum b {
        NONE(0),
        MOBILE(1),
        MOBILE_2G(2),
        MOBILE_3G(3),
        WIFI(4),
        MOBILE_4G(5);

        public b(int i) {
        }
    }

    public static e a(String str, String str2, boolean z) {
        try {
            if (!TextUtils.isEmpty(str2)) {
                if (!TextUtils.isEmpty(str)) {
                    return b(str, str2.getBytes(), a.GZIP, "application/json; charset=utf-8", z);
                }
            }
            return new e(201);
        } catch (Throwable th) {
            Context context = n.a;
            c cVar = n.d;
            return new e(207, th);
        }
    }

    /* JADX WARNING: Missing exception handler attribute for start block: B:14:0x0032 */
    /* JADX WARNING: Failed to process nested try/catch */
    /* JADX WARNING: Missing block: B:15:?, code skipped:
            r1 = c.c.c.a.n.a;
            r1 = c.c.c.a.n.d;
     */
    /* JADX WARNING: Missing block: B:18:0x003f, code skipped:
            r2.close();
     */
    public static c.c.c.a.o.e b(java.lang.String r10, byte[] r11, c.c.c.a.o.b.a r12, java.lang.String r13, boolean r14) throws java.io.IOException {
        /*
        if (r10 != 0) goto L_0x000a;
    L_0x0002:
        r0 = new c.c.c.a.o.e;
        r1 = 201; // 0xc9 float:2.82E-43 double:9.93E-322;
        r0.m1101init(r1);
        return r0;
    L_0x000a:
        r2 = 0;
        if (r11 != 0) goto L_0x0010;
    L_0x000d:
        r3 = new byte[r2];
        goto L_0x0011;
    L_0x0010:
        r3 = r11;
    L_0x0011:
        r4 = r3.length;
        r5 = c.c.c.a.o.b.a.GZIP;
        r6 = 8192; // 0x2000 float:1.14794E-41 double:4.0474E-320;
        r7 = 128; // 0x80 float:1.794E-43 double:6.32E-322;
        r8 = 0;
        if (r5 != r12) goto L_0x0043;
    L_0x001b:
        if (r4 <= r7) goto L_0x0043;
    L_0x001d:
        r1 = new java.io.ByteArrayOutputStream;
        r1.<init>(r6);
        r2 = new java.util.zip.GZIPOutputStream;
        r2.<init>(r1);
        r2.write(r3);	 Catch:{ all -> 0x0032 }
        r2.close();
        r8 = r1.toByteArray();
        goto L_0x0039;
    L_0x0032:
        r1 = c.c.c.a.n.a;	 Catch:{ all -> 0x003e }
        r1 = c.c.c.a.n.d;	 Catch:{ all -> 0x003e }
        r2.close();
    L_0x0039:
        r1 = "gzip";
        r6 = r1;
        r2 = r8;
        goto L_0x0077;
    L_0x003e:
        r0 = move-exception;
        r2.close();
        throw r0;
    L_0x0043:
        r5 = c.c.c.a.o.b.a.DEFLATER;
        if (r5 != r12) goto L_0x0075;
    L_0x0047:
        if (r4 <= r7) goto L_0x0075;
    L_0x0049:
        r1 = new java.io.ByteArrayOutputStream;
        r1.<init>(r6);
        r4 = new java.util.zip.Deflater;
        r4.<init>();
        r4.setInput(r3);
        r4.finish();
        r3 = new byte[r6];
    L_0x005b:
        r5 = r4.finished();
        if (r5 != 0) goto L_0x0069;
    L_0x0061:
        r5 = r4.deflate(r3);
        r1.write(r3, r2, r5);
        goto L_0x005b;
    L_0x0069:
        r4.end();
        r1 = r1.toByteArray();
        r2 = "deflate";
        r6 = r2;
        r2 = r1;
        goto L_0x0077;
    L_0x0075:
        r2 = r3;
        r6 = r8;
    L_0x0077:
        if (r2 != 0) goto L_0x0081;
    L_0x0079:
        r0 = new c.c.c.a.o.e;
        r1 = 202; // 0xca float:2.83E-43 double:1.0E-321;
        r0.m1101init(r1);
        return r0;
    L_0x0081:
        if (r14 == 0) goto L_0x00cc;
    L_0x0083:
        r1 = r2.length;
        r1 = com.bytedance.embedapplog.util.TTEncryptUtils.a(r2, r1);
        if (r1 == 0) goto L_0x00c0;
    L_0x008a:
        r2 = new java.net.URL;
        r2.<init>(r10);
        r2 = r2.getQuery();
        r2 = android.text.TextUtils.isEmpty(r2);
        if (r2 == 0) goto L_0x00a6;
    L_0x0099:
        r2 = "?";
        r3 = r10.endsWith(r2);
        if (r3 != 0) goto L_0x00b3;
    L_0x00a1:
        r0 = c.b.a.a.a.D(r10, r2);
        goto L_0x00b4;
    L_0x00a6:
        r2 = "&";
        r3 = r10.endsWith(r2);
        if (r3 != 0) goto L_0x00b3;
    L_0x00ae:
        r0 = c.b.a.a.a.D(r10, r2);
        goto L_0x00b4;
    L_0x00b3:
        r0 = r10;
    L_0x00b4:
        r2 = "tt_data=a";
        r0 = c.b.a.a.a.D(r0, r2);
        r2 = "application/octet-stream;tt-data=a";
        r3 = r0;
        r4 = r1;
        r5 = r2;
        goto L_0x00c3;
    L_0x00c0:
        r3 = r10;
        r5 = r13;
        r4 = r2;
    L_0x00c3:
        r8 = 1;
        r9 = 1;
        r7 = "POST";
        r0 = c(r3, r4, r5, r6, r7, r8, r9);
        return r0;
    L_0x00cc:
        r7 = 1;
        r8 = 0;
        r5 = "POST";
        r1 = r10;
        r3 = r13;
        r4 = r6;
        r6 = r7;
        r7 = r8;
        r0 = c(r1, r2, r3, r4, r5, r6, r7);
        return r0;
        */
        throw new UnsupportedOperationException("Method not decompiled: c.c.c.a.o.b.b(java.lang.String, byte[], c.c.c.a.o.b$a, java.lang.String, boolean):c.c.c.a.o.e");
    }

    /* JADX WARNING: Removed duplicated region for block: B:41:0x0087 A:{SYNTHETIC, Splitter:B:41:0x0087} */
    /* JADX WARNING: Missing exception handler attribute for start block: B:43:0x008a */
    /* JADX WARNING: Removed duplicated region for block: B:92:0x00fe A:{SYNTHETIC, Splitter:B:92:0x00fe} */
    /* JADX WARNING: Removed duplicated region for block: B:96:0x0105 A:{SYNTHETIC, Splitter:B:96:0x0105} */
    /* JADX WARNING: Removed duplicated region for block: B:92:0x00fe A:{SYNTHETIC, Splitter:B:92:0x00fe} */
    /* JADX WARNING: Removed duplicated region for block: B:96:0x0105 A:{SYNTHETIC, Splitter:B:96:0x0105} */
    /* JADX WARNING: Removed duplicated region for block: B:63:0x00b5 A:{SYNTHETIC, Splitter:B:63:0x00b5} */
    /* JADX WARNING: Missing exception handler attribute for start block: B:65:0x00b8 */
    /* JADX WARNING: Can't wrap try/catch for region: R(4:37|(2:41|42)|43|44) */
    /* JADX WARNING: Can't wrap try/catch for region: R(5:58|59|(2:63|64)|65|66) */
    public static c.c.c.a.o.e c(java.lang.String r4, byte[] r5, java.lang.String r6, java.lang.String r7, java.lang.String r8, boolean r9, boolean r10) {
        /*
        r0 = "gzip";
        r1 = 0;
        r2 = new java.util.LinkedList;	 Catch:{ all -> 0x00ee }
        r2.<init>();	 Catch:{ all -> 0x00ee }
        if (r10 == 0) goto L_0x0013;
    L_0x000a:
        r3 = new java.net.URL;	 Catch:{ all -> 0x00ee }
        android.text.TextUtils.isEmpty(r4);	 Catch:{ all -> 0x00ee }
        r3.<init>(r4);	 Catch:{ all -> 0x00ee }
        goto L_0x0018;
    L_0x0013:
        r3 = new java.net.URL;	 Catch:{ all -> 0x00ee }
        r3.<init>(r4);	 Catch:{ all -> 0x00ee }
    L_0x0018:
        r4 = r3.openConnection();	 Catch:{ all -> 0x00ee }
        r4 = (java.net.HttpURLConnection) r4;	 Catch:{ all -> 0x00ee }
        if (r10 == 0) goto L_0x0048;
    L_0x0020:
        r10 = r2.isEmpty();	 Catch:{ all -> 0x0045 }
        if (r10 != 0) goto L_0x0048;
    L_0x0026:
        r10 = r2.iterator();	 Catch:{ all -> 0x0045 }
    L_0x002a:
        r2 = r10.hasNext();	 Catch:{ all -> 0x0045 }
        if (r2 == 0) goto L_0x0048;
    L_0x0030:
        r2 = r10.next();	 Catch:{ all -> 0x0045 }
        r2 = (android.util.Pair) r2;	 Catch:{ all -> 0x0045 }
        if (r2 != 0) goto L_0x0039;
    L_0x0038:
        goto L_0x002a;
    L_0x0039:
        r3 = r2.first;	 Catch:{ all -> 0x0045 }
        r3 = (java.lang.String) r3;	 Catch:{ all -> 0x0045 }
        r2 = r2.second;	 Catch:{ all -> 0x0045 }
        r2 = (java.lang.String) r2;	 Catch:{ all -> 0x0045 }
        r4.setRequestProperty(r3, r2);	 Catch:{ all -> 0x0045 }
        goto L_0x002a;
    L_0x0045:
        r5 = move-exception;
        goto L_0x00ea;
    L_0x0048:
        if (r9 == 0) goto L_0x004f;
    L_0x004a:
        r9 = 1;
        r4.setDoOutput(r9);	 Catch:{ all -> 0x0045 }
        goto L_0x0053;
    L_0x004f:
        r9 = 0;
        r4.setDoOutput(r9);	 Catch:{ all -> 0x0045 }
    L_0x0053:
        if (r6 == 0) goto L_0x005a;
    L_0x0055:
        r9 = "Content-Type";
        r4.setRequestProperty(r9, r6);	 Catch:{ all -> 0x0045 }
    L_0x005a:
        if (r7 == 0) goto L_0x0061;
    L_0x005c:
        r6 = "Content-Encoding";
        r4.setRequestProperty(r6, r7);	 Catch:{ all -> 0x0045 }
    L_0x0061:
        r6 = "Accept-Encoding";
        r4.setRequestProperty(r6, r0);	 Catch:{ all -> 0x0045 }
        r4.setRequestMethod(r8);	 Catch:{ all -> 0x0045 }
        if (r5 == 0) goto L_0x008b;
    L_0x006b:
        r6 = r5.length;	 Catch:{ all -> 0x0045 }
        if (r6 <= 0) goto L_0x008b;
    L_0x006e:
        r6 = new java.io.DataOutputStream;	 Catch:{ all -> 0x0083 }
        r7 = r4.getOutputStream();	 Catch:{ all -> 0x0083 }
        r6.<init>(r7);	 Catch:{ all -> 0x0083 }
        r6.write(r5);	 Catch:{ all -> 0x0081 }
        r6.flush();	 Catch:{ all -> 0x0081 }
        r6.close();	 Catch:{ all -> 0x008b }
        goto L_0x008b;
    L_0x0081:
        r5 = move-exception;
        goto L_0x0085;
    L_0x0083:
        r5 = move-exception;
        r6 = r1;
    L_0x0085:
        if (r6 == 0) goto L_0x008a;
    L_0x0087:
        r6.close();	 Catch:{ all -> 0x008a }
    L_0x008a:
        throw r5;	 Catch:{ all -> 0x0045 }
    L_0x008b:
        r5 = r4.getResponseCode();	 Catch:{ all -> 0x0045 }
        r6 = 200; // 0xc8 float:2.8E-43 double:9.9E-322;
        if (r5 != r6) goto L_0x00ce;
    L_0x0093:
        r5 = r4.getInputStream();	 Catch:{ all -> 0x0045 }
        r6 = r4.getContentEncoding();	 Catch:{ all -> 0x00cc }
        r6 = r0.equalsIgnoreCase(r6);	 Catch:{ all -> 0x00cc }
        if (r6 == 0) goto L_0x00b9;
    L_0x00a1:
        r6 = new java.util.zip.GZIPInputStream;	 Catch:{ all -> 0x00b1 }
        r6.<init>(r5);	 Catch:{ all -> 0x00b1 }
        r7 = d(r6);	 Catch:{ all -> 0x00ae }
        r6.close();	 Catch:{ all -> 0x00bd }
        goto L_0x00bd;
    L_0x00ae:
        r7 = move-exception;
        r1 = r6;
        goto L_0x00b3;
    L_0x00b1:
        r6 = move-exception;
        r7 = r6;
    L_0x00b3:
        if (r1 == 0) goto L_0x00b8;
    L_0x00b5:
        r1.close();	 Catch:{ all -> 0x00b8 }
    L_0x00b8:
        throw r7;	 Catch:{ all -> 0x00cc }
    L_0x00b9:
        r7 = d(r5);	 Catch:{ all -> 0x00cc }
    L_0x00bd:
        r6 = e(r7);	 Catch:{ all -> 0x00cc }
        r4.disconnect();	 Catch:{ Exception -> 0x00c5 }
        goto L_0x00c6;
    L_0x00c6:
        if (r5 == 0) goto L_0x00cb;
    L_0x00c8:
        r5.close();	 Catch:{ all -> 0x00cb }
    L_0x00cb:
        return r6;
    L_0x00cc:
        r6 = move-exception;
        goto L_0x00ec;
    L_0x00ce:
        r6 = new c.c.c.a.o.e;	 Catch:{ all -> 0x0045 }
        r7 = 206; // 0xce float:2.89E-43 double:1.02E-321;
        r8 = new java.lang.StringBuilder;	 Catch:{ all -> 0x0045 }
        r8.<init>();	 Catch:{ all -> 0x0045 }
        r9 = "http response code ";
        r8.append(r9);	 Catch:{ all -> 0x0045 }
        r8.append(r5);	 Catch:{ all -> 0x0045 }
        r5 = r8.toString();	 Catch:{ all -> 0x0045 }
        r6.m1102init(r7, r5);	 Catch:{ all -> 0x0045 }
        r4.disconnect();	 Catch:{ Exception -> 0x00e9 }
    L_0x00e9:
        return r6;
    L_0x00ea:
        r6 = r5;
        r5 = r1;
    L_0x00ec:
        r1 = r4;
        goto L_0x00f1;
    L_0x00ee:
        r4 = move-exception;
        r6 = r4;
        r5 = r1;
    L_0x00f1:
        r4 = c.c.c.a.n.a;	 Catch:{ all -> 0x0109 }
        r4 = c.c.c.a.n.d;	 Catch:{ all -> 0x0109 }
        r4 = new c.c.c.a.o.e;	 Catch:{ all -> 0x0109 }
        r7 = 207; // 0xcf float:2.9E-43 double:1.023E-321;
        r4.m1103init(r7, r6);	 Catch:{ all -> 0x0109 }
        if (r1 == 0) goto L_0x0103;
    L_0x00fe:
        r1.disconnect();	 Catch:{ Exception -> 0x0102 }
        goto L_0x0103;
    L_0x0103:
        if (r5 == 0) goto L_0x0108;
    L_0x0105:
        r5.close();	 Catch:{ all -> 0x0108 }
    L_0x0108:
        return r4;
    L_0x0109:
        r4 = move-exception;
        if (r1 == 0) goto L_0x0111;
    L_0x010c:
        r1.disconnect();	 Catch:{ Exception -> 0x0110 }
        goto L_0x0111;
    L_0x0111:
        if (r5 == 0) goto L_0x0116;
    L_0x0113:
        r5.close();	 Catch:{ all -> 0x0116 }
    L_0x0116:
        throw r4;
        */
        throw new UnsupportedOperationException("Method not decompiled: c.c.c.a.o.b.c(java.lang.String, byte[], java.lang.String, java.lang.String, java.lang.String, boolean, boolean):c.c.c.a.o.e");
    }

    public static byte[] d(InputStream inputStream) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] bArr = new byte[8192];
        while (true) {
            int read = inputStream.read(bArr);
            if (-1 == read) {
                break;
            }
            byteArrayOutputStream.write(bArr, 0, read);
        }
        inputStream.close();
        try {
            byte[] toByteArray = byteArrayOutputStream.toByteArray();
            try {
                byteArrayOutputStream.close();
            } catch (Throwable unused) {
            }
            return toByteArray;
        } catch (Throwable unused2) {
        }
    }

    public static e e(byte[] bArr) {
        if (bArr == null || bArr.length == 0) {
            return new e(203);
        }
        String str = new String(bArr, Charset.forName("utf-8"));
        try {
            JSONObject jSONObject = new JSONObject(str);
            if (jSONObject.length() > 0) {
                return new e(0, jSONObject);
            }
            return new e(204, str);
        } catch (JSONException unused) {
            return new e(204, str);
        }
    }
}
